﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using 股票监控程序.ViewModels;
using 股票库;

namespace 股票监控程序.Views
{
    /// <summary>
    /// StockView.xaml 的交互逻辑
    /// </summary>
    public partial class StockView : UserControl
    {
        public StockView()
        {
            InitializeComponent();
        }
        public StockInfo StockInfos
        {
            get { return (StockInfo)GetValue(StockInfosProperty); }
            set { SetValue(StockInfosProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StockName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StockInfosProperty =
            DependencyProperty.Register("StockInfos", typeof(StockInfo), typeof(StockView), new PropertyMetadata(null));



        public float 开盘价
        {
            get { return (float)GetValue(开盘价Property); }
            set { SetValue(开盘价Property, value); }
        }

        // Using a DependencyProperty as the backing store for 开盘价.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty 开盘价Property =
            DependencyProperty.Register("开盘价", typeof(float), typeof(StockView), new PropertyMetadata(0f));



        public float 收盘价
        {
            get { return (float)GetValue(收盘价Property); }
            set { SetValue(收盘价Property, value); }
        }

        // Using a DependencyProperty as the backing store for 收盘价.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty 收盘价Property =
            DependencyProperty.Register("收盘价", typeof(float), typeof(StockView), new PropertyMetadata(0f));



        public float 当前价
        {
            get { return (float)GetValue(当前价Property); }
            set { SetValue(当前价Property, value); }
        }

        // Using a DependencyProperty as the backing store for 当前价.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty 当前价Property =
            DependencyProperty.Register("当前价", typeof(float), typeof(StockView), new PropertyMetadata(0f));



        public float 涨幅
        {
            get { return (float)GetValue(涨幅Property); }
            set { SetValue(涨幅Property, value); }
        }

        // Using a DependencyProperty as the backing store for 涨幅.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty 涨幅Property =
            DependencyProperty.Register("涨幅", typeof(float), typeof(StockView), new PropertyMetadata(0f));




        public float 回本
        {
            get { return (float)GetValue(回本Property); }
            set { SetValue(回本Property, value); }
        }

        // Using a DependencyProperty as the backing store for 回本.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty 回本Property =
            DependencyProperty.Register("回本", typeof(float), typeof(StockView), new PropertyMetadata(0f));



        public float 回本涨幅
        {
            get { return (float)GetValue(回本涨幅Property); }
            set { SetValue(回本涨幅Property, value); }
        }

        // Using a DependencyProperty as the backing store for 回本涨幅.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty 回本涨幅Property =
            DependencyProperty.Register("回本涨幅", typeof(float), typeof(StockView), new PropertyMetadata(0f));



        private async void u_Loaded(object sender, RoutedEventArgs e)
        {
            回本 = StockInfos.BJ;
            while (true)
            {
                try
                {
                    string s = await new 股票库.获取股票详情().GetJson(StockInfos.StockName);
                    var res = s.Split(',', StringSplitOptions.RemoveEmptyEntries).Skip(1).Take(3).ToArray();
                    开盘价 = float.Parse(res[0]);
                    收盘价 = float.Parse(res[1]);
                    当前价 = float.Parse(res[2]);
                    涨幅 = (float)Math.Round(当前价 - 收盘价, 3);
                    回本涨幅 = (float)Math.Round(当前价 - 回本, 3);
                    await Task.Delay(1000);
                }
                catch (Exception)
                {
                    await Task.Delay(1000);
                    continue;
                }
               
            }
        }



    }
}
